package com.sdry.web.controller.hpf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Page;
import com.sdry.model.hpf.Part;
import com.sdry.service.hpf.PartService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * @ClassName: PartController 
 * @Description:零件信息Controller
 * @Author hpf
 * @Date 2018年9月20日 下午7:10:20 
 * @version 1.0
 */
@RequestMapping(value="/partMana")
@Controller
public class PartController {

	@Resource
	private PartService partService;
	
	/**
	 * 分页查询零件信息
	 * @param page 分页条件
	 * @param Part 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPagePart")
	public void listPagePart(Page page,Part part,HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (null != part.getPaname() && !"".equals(part.getPaname())) {
			map.put("paname", part.getPaname());
		} else {
			map.put("paname", "");
			part.setPaname("");
		}
		int count = partService.countPart(part);
		List<Part> partList = partService.listPagePart(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(partList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存零件信息
	 * @param Part
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/savePart")
	public void savePart(Part part,HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		if (part.getPaid() == null || "".equals(part.getPaid())) {
			isSuccess = partService.insertPart(part);
		} else {
			isSuccess = partService.updatePart(part);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除零件信息
	 * @param str 配件信息ID组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deletePart")
	public void deletePart(@RequestParam("str") String str,HttpServletResponse response) throws Exception{
		int isSuccess = partService.deletePart(str);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 校验是否删除配件信息
	 * @param str 配件信息ID组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deletePartOfVerify")
	public void deletePartOfVerify(@RequestParam("str") String str,HttpServletResponse response) throws Exception{
		int affectRow = partService.deletePartOfVerify(str);
		ResponseUtil.write(response, affectRow);
		
	}
}